﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.EntityModel;

namespace DataAccess
{
    public static class InterveningPoliceDataAccess
    {
        public static InterveningPolice InsertInterveningPolice(InterveningPolice newPerson)
        {
            BrigadaEntities context = new BrigadaEntities();
            context.AddToInterveningPolice(newPerson);
            context.SaveChanges();
            return newPerson;
        }

        public static bool PoliceAlreadyExists(long ciPerson)
        {
            BrigadaEntities context = new BrigadaEntities();
            var person = (from p in context.InterveningPolice
                          where p.CI == ciPerson
                          select p).SingleOrDefault();
            return (person != null);
        }

        public static InterveningPolice GetPoliceByCi(long ciPerson)
        {
            BrigadaEntities context = new BrigadaEntities();
            var person = (from p in context.InterveningPolice
                          where p.CI == ciPerson
                          select p).SingleOrDefault();
            return person;
        }

        public  static InterveningPolice UpdateInterveningPolice(long ci,InterveningPolice ip)
        {
            BrigadaEntities context = new BrigadaEntities();
            var police = (from p in context.InterveningPolice
                          where p.CI == ip.CI
                          select p).Single();
            police.CI = ci;
            police.Name = ip.Name;
            police.PaternalName = ip.PaternalName;
            police.MaternalName = ip.MaternalName;
            police.Rank = ip.Rank;
            police.PoliceStation=ip.PoliceStation;
            police.Place=ip.Place;
            police.Zone = ip.Zone;
            context.SaveChanges();
            return police;
        }

        public static void DeleteInterveningPolice(long ci)
        {
            BrigadaEntities context = new BrigadaEntities();
            var complaint = (from c in context.InterveningPolice
                             where c.CI == ci
                             select c).SingleOrDefault();
            context.InterveningPolice.DeleteObject(complaint);
            context.SaveChanges();
        }
    }
}
